package com.wzvtc.vsl.securitytool;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.wzvtc.vsl.securitytool.utils.MD5Utils;

/**
 * Created by Vsl on 2015/3/21.
 */
public class SafeFragment extends Fragment {
    private LinearLayout ll_safe_safe;
    private EditText et_set_password;
    private EditText et_confirm_password;
    private Button ok;
    private Button cancel;
    private AlertDialog dialog;
    private SharedPreferences sp;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.tab1_safe, container, false);

        ll_safe_safe = (LinearLayout) view.findViewById(R.id.ll_safe_safe);
        sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);

        ll_safe_safe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLostFindDialog();
            }
        });

        return view;
    }

    private void showLostFindDialog() {
        if (isSetPassword()) {
            showEnterDialog();
        } else {
            showSetPasswordDialog();
        }
    }

    private void showSetPasswordDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = View.inflate(getActivity(), R.layout.dialog_set_password, null);

        et_set_password = (EditText) view.findViewById(R.id.et_set_password);
        et_confirm_password = (EditText) view.findViewById(R.id.et_confirm_password);
        ok = (Button) view.findViewById(R.id.ok);
        cancel = (Button) view.findViewById(R.id.cancel);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = et_set_password.getText().toString().trim();
                String password_confirm = et_confirm_password.getText().toString().trim();
                if (TextUtils.isEmpty(password) || TextUtils.isEmpty(password_confirm)) {
                    Toast.makeText(getActivity(), "密码为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password.equals(password_confirm)) {
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("password", MD5Utils.md5Password(password));
                    editor.commit();
                    dialog.dismiss();
                    Intent intent = new Intent(getActivity(), LostFindActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(getActivity(), "密码不一致！", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });

        //builder.setView(view);
        dialog = builder.create();
        dialog.setView(view, 0, 0, 0, 0);
        dialog.show();
    }

    private void showEnterDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = View.inflate(getActivity(), R.layout.dialog_enter_password, null);

        et_set_password = (EditText) view.findViewById(R.id.et_set_password);
        ok = (Button) view.findViewById(R.id.ok);
        cancel = (Button) view.findViewById(R.id.cancel);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = et_set_password.getText().toString().trim();
                String savePassword = sp.getString("password", "");
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(getActivity(), "密码为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (MD5Utils.md5Password(password).equals(savePassword)) {
                    dialog.dismiss();
                    Intent intent = new Intent(getActivity(), LostFindActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(getActivity(), "密码错误！", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });

        //builder.setView(view);
        dialog = builder.create();
        dialog.setView(view, 0, 0, 0, 0);
        dialog.show();
    }

    private boolean isSetPassword() {
        String password = sp.getString("password", null);
        return !TextUtils.isEmpty(password);
    }
}
